//
//  MSCommentView.m
//  MSJD
//
//  Created by xiangbo on 2018/10/21.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "MSCommentView.h"
#import "MSCollectionBaseCell.h"
#import "UILabel+AutoSize.h"

@interface MSCommentView()<UICollectionViewDelegate,UICollectionViewDataSource,MSDetailViewDelegate>

@end

@implementation MSCommentView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews{
    self.headImg = [[UIImageView alloc] init];
    self.headImg.backgroundColor = MSSubTitleColor;
    self.headImg.layer.cornerRadius = 20.f;
    [self addSubview:self.headImg];
    
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.font = [UIFont systemFontOfSize:15];
    self.nameLabel.text = @"我是小草";
    self.nameLabel.textColor = MSTitleColor;
    [self addSubview:self.nameLabel];
    
    self.timeLabel = [[UILabel alloc] init];
    self.timeLabel.textColor = MSSubTitleColor;
    self.timeLabel.font = [UIFont systemFontOfSize:11];
    self.timeLabel.text = @"2小时以前";
    [self addSubview:self.timeLabel];
    
    UILabel *scoreDescLabel = [[UILabel alloc] init];
    scoreDescLabel.text = @"打分";
    scoreDescLabel.font = [UIFont systemFontOfSize:12.f];
    scoreDescLabel.textColor = UIColorHex(0x444444);
    [self addSubview:scoreDescLabel];
    
    self.scoreView = [[MSScoreView alloc] init];
    [self addSubview:self.scoreView];
    
    self.contentLabel = [[UILabel alloc] init];
    self.contentLabel.font = [UIFont systemFontOfSize:15];
    self.contentLabel.text = @"content";
    self.contentLabel.textColor = MSTitleColor;
    self.contentLabel.numberOfLines = 3;
    [self addSubview:self.contentLabel];
    
    [self addSubview:self.collectionView];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = UIColorHex(0xdddddd);
    [self addSubview:lineView];
    
    [self.headImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(9);
        make.left.equalTo(self).offset(10);
        make.width.height.mas_equalTo(40);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headImg.mas_right).offset(8);
        make.bottom.equalTo(self.headImg.mas_centerY).offset(-3);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel);
        make.top.equalTo(self.headImg.mas_centerY).offset(3);
    }];
    
    [scoreDescLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headImg.mas_bottom).offset(8);
        make.left.equalTo(self.nameLabel);
        make.height.mas_equalTo(12);
    }];
    
    [self.scoreView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(scoreDescLabel);
        make.left.equalTo(scoreDescLabel.mas_right).offset(3);
        make.right.equalTo(self);
        make.height.equalTo(scoreDescLabel.mas_height);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel);
        make.right.equalTo(self).offset(-8);
        make.top.equalTo(scoreDescLabel.mas_bottom).offset(16);
    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel);
        make.top.equalTo(self.contentLabel.mas_bottom);
        make.right.equalTo(self).offset(-8);
        make.bottom.equalTo(self).offset(-8);;
    }];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headImg);
        make.right.equalTo(self.contentLabel);
        make.bottom.equalTo(self);
        make.height.mas_equalTo(0.5);
    }];
}

- (void)configData:(id)data indexPath:(NSIndexPath *)indexPath{
    self.comment = data;
}
- (void)setComment:(MSCommentlistRowsModel *)comment{
    _comment = comment;
    [self.headImg setImageURL:[NSURL URLWithString:comment.user.avatar]];
    self.nameLabel.text = comment.user.username;
    self.timeLabel.text = comment.createdAt;
    self.scoreView.score = @(comment.score.floatValue);
    self.contentLabel.text = comment.content;
    if (comment.height == 0) {
        CGFloat height = [UILabel getHeightByWidth:MS_SCREEN_WIDTH - 66 title:comment.content font:self.contentLabel.font];
        height = MIN(height, 54);
        CGFloat externHeight =  self.comment.pictures.count > 0 ? 86 : 0;
        
        comment.height = height + 91 + externHeight;
    }
    
    [self.collectionView reloadData];
    
}

#pragma mark UITableViewDelegate and DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.comment.pictures.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MSPhotosCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(MSPhotosCollectionViewCell.class) forIndexPath:indexPath];
    MSCommentPictures *picture = self.comment.pictures[indexPath.row];
    [cell.image setImageWithURL:[NSURL URLWithString:picture.url] placeholder:[UIImage imageWithColor:MSPlaceholderColor]];
    return cell;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(8, 0, 8, 0);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(70.f, 70.f);
}

#pragma mark -Property
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 5;
        layout.minimumInteritemSpacing = 5;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        [_collectionView registerClass:[MSPhotosCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([MSPhotosCollectionViewCell class])];
        
    }
    return _collectionView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
